$(function () {
    $("#jqGrid").Grid({
        url: '../orderdrawback/list',
        colModel: [
			{label: '订单编号', name: 'orderNo', index: 'order_no', key: true, width: 80},
			{label: '用户编号', name: 'uid', index: 'uid', width: 80},
			{label: '手机号', name: 'phone', index: 'phone', width: 80},
			{label: '原因', name: 'cause', index: 'cause', width: 80},
			{label: '商品编号', name: 'proId', index: 'pro_id', width: 80},
			{label: '商品状态', name: 'proStatus', index: 'pro_status', width: 80},
			{label: '状态名称', name: 'proStatusName', index: 'pro_status_name', width: 80},
			{label: '金额', name: 'amount', index: 'amount', width: 80},
			{label: '退款金额', name: 'backAmount', index: 'back_amount', width: 80},
			{label: '说明', name: 'explain', index: 'explain', width: 80},
			//{label: '凭证', name: 'certificate', index: 'certificate', width: 80},
			{label: '状态', name: 'statu', index: 'statu', width: 80,
				formatter: function (value) {
					if (value == '1') {
						return "<span class=\"label label-warning\">申请中</span>";
					} else if (value == '2') {
						return "<span class=\"label label-info\">通过</span>";
					} else if (value == '3') {
						return "<span class=\"label label-success\">完成</span>";
					}
					return value;
				}
			},
			{label: '类型', name: 'type', index: 'type', width: 80},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80},
			{label: '修改时间', name: 'updateDate', index: 'update_date', width: 80}]
    });

	Ajax.request({
		url: "../sys/merchant/queryMerchant",
		async: true,
		successCallback: function (r) {
			vm.merchants = [];
			vm.merchants = r.list;
		}
	});
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		statued:'',
		merchants : [],
		tOrderDrawback: {},
		certificates :[],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    phone: '',
			orderno:'',
			statu:'',
			merchantId:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.tOrderDrawback = {};
		},
		update: function (event) {
            let orderNo = getSelectedRow("#jqGrid");
			if (orderNo == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(orderNo)
		},
		saveOrUpdate: function (event) {
			if (vm.tOrderDrawback.statu < vm.statued){
				if (vm.statued == 2) {
					iview.Message.error('订单已通过!');
				}else {
					iview.Message.error('订单已完成!');
				}
				return;
			}
            let url = vm.tOrderDrawback.orderNo == null ? "../orderdrawback/save" : "../orderdrawback/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.tOrderDrawback),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let orderNos = getSelectedRows("#jqGrid");
			if (orderNos == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../orderdrawback/delete",
                    params: JSON.stringify(orderNos),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(orderNo){
            Ajax.request({
                url: "../orderdrawback/info/"+orderNo,
                async: true,
                successCallback: function (r) {
                    vm.tOrderDrawback = r.tOrderDrawback;
					vm.certificates = r.tOrderDrawback.certificates;
					vm.statued = r.tOrderDrawback.statu;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.statued = '';
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'phone': vm.q.phone,'orderno':vm.q.orderno,'statu':vm.q.statu,"merchantId":vm.q.merchantId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
				phone: '',
				orderno:'',
				statu:'',
				merchantId:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});